package com.bage.finance.biz.service.impl;

import com.bage.finance.biz.domain.MemberBindWxOpenId;
import com.bage.finance.biz.mapper.MemberBindWxOpenIdMapper;
import com.bage.finance.biz.service.MemberBindWxOpenIdService;
import com.bage.mybatis.help.MyBatisWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.bage.finance.biz.domain.MemberBindPhoneField.MemberId;
import static com.bage.finance.biz.domain.MemberBindWxOpenIdField.setAppId;
import static com.bage.finance.biz.domain.MemberBindWxOpenIdField.setOpenId;

@Slf4j
@Service
@RequiredArgsConstructor
public class MemberBindWxOpenIdServiceImpl implements MemberBindWxOpenIdService {

    final MemberBindWxOpenIdMapper memberBindWxOpenIdMapper;

    @Override
    public MemberBindWxOpenId get(String appId, String openId) {
        MyBatisWrapper<MemberBindWxOpenId> myBatisWrapper = new MyBatisWrapper<>();
        myBatisWrapper.select(MemberId)
                .whereBuilder()
                .andEq(setAppId(appId))
                .andEq(setOpenId(openId));
        return memberBindWxOpenIdMapper.topOne(myBatisWrapper);
    }

    @Override
    public boolean reg(String appId, String openId, long memberId) {
        MemberBindWxOpenId memberBindWxOpenId = new MemberBindWxOpenId();
        memberBindWxOpenId.setAppId(appId);
        memberBindWxOpenId.setOpenId(openId);
        memberBindWxOpenId.setMemberId(memberId);
        memberBindWxOpenId.initDefault();
        return memberBindWxOpenIdMapper.insertSelective(memberBindWxOpenId) > 0;
    }
}
